import request from '../utils/request'
import API from './contants'

/**
 * 查询所有分类
 * @returns 所有分类数据
 */
export const getAllCategories = () => {
  return request({
    url: API.TABS_API,
    method: 'GET',
  })
}

/**
 * 根据主分类查询子分类数据
 * @param {*} id 主分类 id
 * @returns
 */
export const getSubCategories = (id, start) => {
  return request({
    url: API.TAB_API + '/' + id,
    method: 'GET',
    params: {
      start,
    },
  })
}

/**
 * 根据 url 获取分类列表商品数据
 * @param {*} url
 */
export const getList = (url, start = 0) => {
  let api
  if (url.startsWith('youxuan://commoditylist')) {
    api = API.LIST_CATEGORY_API
  } else if (url.startsWith('youxuan://column')) {
    api = API.LIST_COLUMN_API
  }
  const id = url.slice(url.lastIndexOf('=') + 1)
  api = api.replace('??', id)
  return request({
    url: api,
    params: { start },
    method: 'GET',
  }).then(data => data.items || data)
}

/**
 * 获取分类下的推荐商品
 * @param {*} id
 * @param {*} start
 * @returns
 */
export const getRecommendProducts = (id, start = 0) => request({
  url: API.TAB_API + '/' + id,
  method: 'GET',
  params: { start },
})
